package com.zjweu.hkg.DAO;

import com.zjweu.hkg.POJO.Message;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MessageDAO {

    @Insert("insert into Message (message,fromId,toId,sendTime,isRead,type) values(#{message},#{fromId},#{toId},now(),#{isRead},#{type})")
     int insertMessage(Message message);
    @Select("select * from Message where (fromId=#{fromId} and toId=#{toId}) or (fromId=#{toId} and toId=#{fromId}) order by sendTime asc")
    List<Message> getHistoryMessageByFromIdAndToId(int fromId, int toId);

    /**
     * 根据用户 ID 查询与之聊天过的人的 ID 列表
     * @param userId 用户 ID
     * @return 聊天过的人的 ID 列表
     */
    @Select("SELECT DISTINCT CASE WHEN fromId = #{userId} THEN toId ELSE fromId END AS chatUserId " +
            "FROM Message WHERE fromId = #{userId} OR toId = #{userId}")
    List<Integer> getChatUserIdsByUserId(int userId);

   @Update("update Message set isRead=1 where msgId=#{msgId}")
    int updateMessageIsReadByMsgId(int msgId);

    @Select("select * from Message where toId=#{userId} and isRead=0")
    List<Message> getUnreadMsgByUserId(int userId);
}
